/**
 * 
 */
package com.antilia.infomaresme.jobs.impl;

import java.util.Date;
import java.util.List;

import org.apache.wicket.util.collections.MiniMap;
import org.apache.wicket.util.template.TextTemplate;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dao.interfaces.IItemEventsDao;
import com.antilia.infomaresme.data.dao.interfaces.IUserItemAlertsDao;
import com.antilia.infomaresme.data.dataobjects.EventStatus;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.ItemEvent;
import com.antilia.infomaresme.data.dataobjects.ItemEventSubject;
import com.antilia.infomaresme.data.dataobjects.Language;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.data.dataobjects.UserItemAlert;
import com.antilia.infomaresme.jobs.IJob;
import com.antilia.infomaresme.mail.MailUtil;
import com.google.inject.Inject;
import com.wideplay.warp.persist.WorkManager;

/**
 * @author Ernesto
 *
 */
public class UserItemsAlertJob implements IJob {

	private static final long serialVersionUID = 1L;

	
	/**
	 * File where it is included the latest date
	 */
	private String file;

	@Inject
	private IItemEventsDao dao;
	
	@Inject
	private IUserItemAlertsDao alertsDao;
	
	@Inject
	protected WorkManager workManager;
	
	/**
	 * 
	 */
	public UserItemsAlertJob() {		
		InfomaresmeApplication.injector.injectMembers(this);
	}

	/* (non-Javadoc)
	 * @see com.antilia.infomaresme.jobs.IJob#execute()
	 */
	public void execute() throws Exception {
		//TODO  fix the date read it for a file 
		try {
			workManager.beginWork();
			List<ItemEvent> events =  dao.getEventsAfter(new Date(0));
			for(ItemEvent event: events) {
				Item item = event.getItem();
				List<UserItemAlert> alerts = getAlertsDao().findAlerts(item);
				for(UserItemAlert alert: alerts) {
					if(alert.getEventsAsEnum().contains(event.getSubject())) {
						//send email.
						sendEmail(alert, event);
					}				
				}
				// close the event
				event.setStatus(EventStatus.closed);
				dao.save(event);				
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(workManager != null)
				workManager.endWork();
		}
	}
	
	public void sendEmail(UserItemAlert alert, ItemEvent event) throws Exception {
		User user = alert.getUser();
		Language language = user.getLanguage();
		MiniMap<String, String> map = new MiniMap<String, String>(5);
		map.put("name", user.getName());
		map.put("title", alert.getItem().getTitle());		
		map.put("old", event.getOldValue());
		map.put("new", event.getNewValue());
		map.put("type", getType(language, event.getSubject()));		
		TextTemplate template = new PackageTextTemplate(UserItemsAlertJob.class, 
				"ItemsAlertMessage_"+language.name()+".template");
		MailUtil.sendEmail( 
				user.getEmail(), 
				getSubject(language, alert), 
				template.asString(map));
	}
	
	protected String getType(Language language, ItemEventSubject subject) {
		if(language.equals(Language.en)) {
			if(subject.equals(ItemEventSubject.price_up)||subject.equals(ItemEventSubject.price_up))
				return "price";
			if(subject.equals(ItemEventSubject.date_changed))
				return "date";
			if(subject.equals(ItemEventSubject.status_changed))
				return "status";
			if(subject.equals(ItemEventSubject.news_added))
				return "news";
		}
		else {
			if(subject.equals(ItemEventSubject.price_up)||subject.equals(ItemEventSubject.price_up))
				return "precio";
			if(subject.equals(ItemEventSubject.date_changed))
				return "fecha";
			if(subject.equals(ItemEventSubject.status_changed))
				return "estado";
			if(subject.equals(ItemEventSubject.news_added))
				return "noticias";
		}
		return "";
	}
	
	protected String getSubject(Language language, UserItemAlert alert) {
		if(language.equals(Language.en))
			return "Your alert "+alert.getTitle();
		else 
			return "Su alerta "+alert.getTitle();
	}
	
	
	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}

	public IItemEventsDao getDao() {
		return dao;
	}

	public void setDao(IItemEventsDao dao) {
		this.dao = dao;
	}

	public void setAlertsDao(IUserItemAlertsDao alertsDao) {
		this.alertsDao = alertsDao;
	}

	public IUserItemAlertsDao getAlertsDao() {
		return alertsDao;
	}

}
